<?php

/**
 # com_jvframwork - JV Framework
 # @version		1.5.x
 # ------------------------------------------------------------------------
 # author    Open Source Code Solutions Co
 # copyright Copyright (C) 2011 joomlavi.com. All Rights Reserved.
 # @license - http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL or later.
 # Websites: http://www.joomlavi.com
 # Technical Support:  http://www.joomlavi.com/my-tickets.html
-------------------------------------------------------------------------*/
// no direct access
defined('_JEXEC') or die('Restricted access');
jimport( 'joomla.html.parameter' );

class JVParameter extends JParameter {
    
    
    /**
	 * Constructor
	 *
	 * @access	protected
	 * @param	string The raw parms text
	 * @param	string Path to the xml setup file
	 * @since	1.5
	 */
	function __construct($data, $path = '')
	{
		parent::__construct('_default');
        $this->_defaultNameSpace = '_default';
                        
		// Set base path
		$this->_elementPath[] = dirname( __FILE__ ).DS.'parameter'.DS.'element';
                        
		if ($data = trim($data)) {
			if (strpos($data, '{') === 0) {
				$this->loadJSON($data);
			} else {
				$this->loadINI($data);
			}
		}

		if ($path) {
			$this->loadSetupFile($path);
		}

		$this->_raw = $data;
	}
    /**
     * Render
     *
     * @access	public
     * @param	string	The name of the control, or the default text area if a setup file is not found
     * @return	string	HTML
     * @since	1.5
     */
    function render($name = 'params', $group = '_default') {
        if (!isset($this->_xml[$group])) {
            return false;
        }

        $params = $this->getParams($name, $group);
        $html = array();
        $html[] ="<div class='list'>";

        if ($description = $this->_xml[$group]->attributes('description')) {
            // add the params description to the display
            $desc = JText::_($description);
            $html[] = '<div class="paramlist_description" >' . $desc . '</div>';
        }

        foreach ($params as $param) {
            $html[] = '<div class="paramrow" >';
            if ($param[0]) {
                $html[] = $param[0];
                $html[] = $param[1];
            } else {
                $html[] = $param[1];
            }
            $html[] = '</div>';
        }

        if (count($params) < 1) {
            $html[] = "<div </div>><i>" . JText::_('There are no Parameters for this item') ."</i></div>";
        }

        $html[] = '</div>';

        return implode("\n", $html);
    }
    
    /**
	 * Load an INI string into the registry into the given namespace [or default if a namespace is not given]
	 *
	 * @access	public
	 * @param	string	$data		INI formatted string to load into the registry
	 * @param	string	$namespace	Namespace to load the INI string into [optional]
	 * @return	boolean True on success
	 * @since	1.5
	 */
	function loadJSON($data, $namespace = null)
	{
	   	   
	   // If namespace is not set, get the default namespace
		if ($namespace == null) {
			$namespace = $this->_defaultNameSpace;
		}

		if (!isset($this->_registry[$namespace])) {
			// If namespace does not exist, make it and load the data
			$this->makeNameSpace($namespace);
			$this->data = $this->_registry[$namespace]['data'] =& json_decode($data);
		} else {
			// Get the data in object format
			$ns = json_decode($data);

			/*
			 * We want to leave groups that are already in the namespace and add the
			 * groups loaded into the namespace.  This overwrites any existing group
			 * with the same name
			 */
            if(is_object($ns))
			foreach (get_object_vars($ns) as $k => $v) {
				$this->_registry[$namespace]['data']->$k = $v;
			}                        
		}

		return true;
	}
}

?>
